package com.ly.ocw.web.admin;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springside.modules.utils.Collections3;

import com.ly.ocw.comm.DateUtil;
import com.ly.ocw.entity.weixin.WeixinUserCardStat;
import com.ly.ocw.service.weixin.WeixinConf;
import com.ly.ocw.service.weixin.WeixinUserCardStatService;

/**
 * 会员卡统计 User: Peter Date: 12/20/12 Time: 5:27 PM
 */
@Controller
@RequestMapping("/admin/stat")
public class StatController {

	private static Logger logger = LoggerFactory.getLogger(StatController.class);

	@Autowired
	private WeixinConf weixinConf;

	@Autowired
	private WeixinUserCardStatService weixinUserCardStatService;
	
	@RequestMapping(value="sms",method = RequestMethod.GET)
	public String sms() {
		logger.info("toStatPage");

		return "admin/stat/sms";
	}


	@RequestMapping(method = RequestMethod.GET)
	public String toStatPage() {
		logger.info("toStatPage");

		return "admin/stat/stat";
	}

	@RequestMapping(value = "statForYear")
	public String statForYear(@ModelAttribute("weixinUserCardStat") WeixinUserCardStat weixinUserCardStat, Model model) {
		logger.info("statForYear year:" + weixinUserCardStat.getYear() + "   cardId:" + weixinUserCardStat.getCardId());

		if (StringUtils.isBlank(weixinUserCardStat.getYear())) {
			weixinUserCardStat.setYear(DateUtil.sdf_4.format(new Date()));
		}
		String title = weixinUserCardStat.getYear() + "年领取会员卡统计";
		String seriesData = "20000, 30000, 50000, 45990,0, 39000, 36000, 5000,0";
		String xAxisData = "'1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'";

		// 月份统计
		if (StringUtils.isNotBlank(weixinUserCardStat.getYearMonth())) {
			
			
			xAxisData = "'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30'";
			
			// 
			int dayOfMonth = getMonthDay(weixinUserCardStat);
			
			xAxisData = initXAxisData(weixinUserCardStat, dayOfMonth);

			String yearMonth = weixinUserCardStat.getYear() + "-" + weixinUserCardStat.getYearMonth();
			title = weixinUserCardStat.getYear() + "年"+ weixinUserCardStat.getYearMonth()+"月领取会员卡统计";
			
			List<WeixinUserCardStat> list = weixinUserCardStatService.statForMonth(yearMonth, weixinUserCardStat.getCardId());

			// 月份 n天 ，没有的补0
			seriesData = processSeriesDataForMonth(list, dayOfMonth);

		}
		// 年份统计
		else {
			List<WeixinUserCardStat> list = weixinUserCardStatService.statForYear(weixinUserCardStat.getYear(), weixinUserCardStat.getCardId());

			// 一年12个月份，没有的补0
			seriesData = processSeriesDataForYear(list);

		}

		logger.info("seriesData:" + seriesData);
		model.addAttribute("year", weixinUserCardStat.getYear());
		model.addAttribute("yearMonth", weixinUserCardStat.getYearMonth());
		model.addAttribute("title", title);
		model.addAttribute("seriesData", seriesData);
		model.addAttribute("xAxisData", xAxisData);
		return "admin/stat/stat";
	}

	// 月份 n天 ，没有的补0
	private String processSeriesDataForMonth(List<WeixinUserCardStat> list, int dayOfMonth) {

		for (int i = 1; i <= dayOfMonth; i++) {
			WeixinUserCardStat o = new WeixinUserCardStat("0", formatDayOfMonth(dayOfMonth));
			if (!list.contains(o)) {
				list.add(o);
			}
		}

		Collections.sort(list);
		return Collections3.extractToString(list, "number", ", ");
	}

	/*
	 * 格式为两位 ，如：01
	 */
	private String formatDayOfMonth(int dayOfMonth) {
		if (dayOfMonth < 10) {
			return "0" + dayOfMonth;
		} else {
			return "" + dayOfMonth;
		}
	}

	/**
	 * x轴的坐标系标尺刻度（月份）
	 * "'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30'"
	 * ;
	 * 
	 * @param weixinUserCardStat
	 * @return
	 */
	private String initXAxisData(WeixinUserCardStat weixinUserCardStat, int dayOfMonth) {
		String xAxisData = "";

		for (int i = 1; i <= dayOfMonth; i++) {
			if (i == dayOfMonth) {
				xAxisData += "'" + i + "'";
			} else {
				xAxisData += "'" + i + "',";
			}
		}

		return xAxisData;
	}

	private int getMonthDay(WeixinUserCardStat weixinUserCardStat) {
		Calendar cal = Calendar.getInstance();
		int year = Integer.valueOf(weixinUserCardStat.getYear());
		cal.set(Calendar.YEAR, year);

		int month = Integer.valueOf(weixinUserCardStat.getYearMonth());
		cal.set(Calendar.MONTH, month-1);

		int dayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 用户选择月份的
														// 天数（28、29、30或31）
		return dayOfMonth;
	}
	
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		int year = Integer.valueOf("2014");
		cal.set(Calendar.YEAR, year);

		int month = Integer.valueOf("02");
		cal.set(Calendar.MONTH, month-1);
		
		System.out.println(cal.get(Calendar.YEAR));
		System.out.println(cal.get(Calendar.MONTH));

		int dayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 用户选择月份的
														// 天数（28、29、30或31）
		System.out.println(dayOfMonth);
	}

	// 一年12个月份，没有的补0
	public String processSeriesDataForYear(List<WeixinUserCardStat> list) {
		WeixinUserCardStat o_1 = new WeixinUserCardStat("0", "01");
		if (!list.contains(o_1)) {
			list.add(o_1);
		}
		WeixinUserCardStat o_2 = new WeixinUserCardStat("0", "02");
		if (!list.contains(o_2)) {
			list.add(o_2);
		}
		WeixinUserCardStat o_3 = new WeixinUserCardStat("0", "03");
		if (!list.contains(o_3)) {
			list.add(o_3);
		}
		WeixinUserCardStat o_4 = new WeixinUserCardStat("0", "04");
		if (!list.contains(o_4)) {
			list.add(o_4);
		}
		WeixinUserCardStat o_5 = new WeixinUserCardStat("0", "05");
		if (!list.contains(o_5)) {
			list.add(o_5);
		}
		WeixinUserCardStat o_6 = new WeixinUserCardStat("0", "06");
		if (!list.contains(o_6)) {
			list.add(o_6);
		}
		WeixinUserCardStat o_7 = new WeixinUserCardStat("0", "07");
		if (!list.contains(o_7)) {
			list.add(o_7);
		}
		WeixinUserCardStat o_8 = new WeixinUserCardStat("0", "08");
		if (!list.contains(o_8)) {
			list.add(o_8);
		}
		WeixinUserCardStat o_9 = new WeixinUserCardStat("0", "09");
		if (!list.contains(o_9)) {
			list.add(o_9);
		}
		WeixinUserCardStat o_10 = new WeixinUserCardStat("0", "10");
		if (!list.contains(o_10)) {
			list.add(o_10);
		}
		WeixinUserCardStat o_11 = new WeixinUserCardStat("0", "11");
		if (!list.contains(o_11)) {
			list.add(o_11);
		}
		WeixinUserCardStat o_12 = new WeixinUserCardStat("0", "12");
		if (!list.contains(o_12)) {
			list.add(o_12);
		}
		Collections.sort(list);
		return Collections3.extractToString(list, "number", ", ");
	}
}
